//
//  NYMineNormalCell.m
//  NYTYY
//
//  Created by MinLison on 2017/12/8.
//  Copyright © 2017年 NYtyy. All rights reserved.
//

#import "NYMineNormalCell.h"

#import "NYAgreementViewController.h"

@interface NYMineNormalCell ()
@property(nonatomic, strong) TTTAttributedLabel *leftTitleLabel;
@property(nonatomic, strong) UIImageView *rightArrowView;
@property(nonatomic, strong) UIImageView *leftIconView;
@property(nonatomic, strong) UIView *bottomLineView;
@end

@implementation NYMineNormalCell

- (void)configure
{
        [super configure];
        self.accessoryType = UITableViewCellAccessoryNone;
        [self.contentView addSubview:self.leftTitleLabel];
        [self.contentView addSubview:self.rightArrowView];
        UIView *bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = UIColorHex(0xe6e6e6);
        self.bottomLineView = bottomLineView;
        [self.contentView addSubview:bottomLineView];
        [self.contentView addSubview:self.leftIconView];
        
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self);
        }];
        [self.leftIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(__WGWidth(16));
                make.centerY.equalTo(self.contentView);
                make.height.width.mas_equalTo(__WGHeight(26));
        }];
        [self.leftTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.leftIconView.mas_right).offset(__WGWidth(10));
                make.centerY.equalTo(self.leftIconView);
        }];
        [self.rightArrowView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.contentView.mas_right).offset(-__WGWidth(19));
                make.centerY.equalTo(self.contentView);
        }];
        [self.bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.contentView);
                make.height.mas_equalTo(1);
                make.left.equalTo(self.leftIconView);
                make.right.equalTo(self.rightArrowView);
        }];
}
- (void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
{
        if ([self.rowDescriptor.opaque boolValue])
        {
                [NYUserManager pushOrPresentLoginIfNeed:YES inViewController:self.formViewController completion:^{
                        
                } dismiss:^{
                        if (NYUserManager.isLogin) {
                                if ([self.rowDescriptor.title isEqualToString:@"服务协议"]) {
                                        
                                        [controller.navigationController pushViewController:[NYAgreementViewController webViewControllerWithUrlString:@"http://api.niuwaketang.com/agreement/"] animated:true];
                                } else {
                                        
                                        [super formDescriptorCellDidSelectedWithFormController:controller];
                                }
                        }
                }];
        }
        else
        {
                [super formDescriptorCellDidSelectedWithFormController:controller];
        }
}
- (void)update
{
//        [super update];
        self.leftTitleLabel.text = self.rowDescriptor.title;
        self.leftIconView.image = self.rowDescriptor.image;
        if (self.rowDescriptor.image == nil)
        {
                [self.leftIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(self.contentView).offset(__WGWidth(16));
                        make.centerY.equalTo(self.contentView);
                        make.height.width.mas_equalTo(__WGHeight(0));
                }];
                [self.leftTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(self.leftIconView.mas_right).offset(__WGWidth(0));
                        make.centerY.equalTo(self.leftIconView);
                }];
                [self.bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.right.bottom.equalTo(self.contentView);
                        make.height.mas_equalTo(1);
                }];
        }
        else
        {
                [self.leftIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(self.contentView).offset(__WGWidth(16));
                        make.centerY.equalTo(self.contentView);
                        make.height.width.mas_equalTo(__WGHeight(26));
                }];
                [self.leftTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(self.leftIconView.mas_right).offset(__WGWidth(10));
                        make.centerY.equalTo(self.leftIconView);
                }];
                [self.bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.bottom.equalTo(self.contentView);
                        make.height.mas_equalTo(1);
                        make.left.equalTo(self.leftIconView);
                        make.right.equalTo(self.rightArrowView);
                }];
        }
}
- (UIImageView *)rightArrowView
{
        if (_rightArrowView == nil) {
                _rightArrowView = [[UIImageView alloc] initWithImage:[UIImage jian_tou]];
        }
        return _rightArrowView;
}
- (UIImageView *)leftIconView
{
        if (_leftIconView == nil) {
                _leftIconView = [[UIImageView alloc] initWithImage:[UIImage jian_tou]];
        }
        return _leftIconView;
}
- (TTTAttributedLabel *)leftTitleLabel
{
        if (_leftTitleLabel == nil) {
                _leftTitleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _leftTitleLabel.font = WGSystem16Font;
                _leftTitleLabel.textColor = [UIColor blackColor];
        }
        return _leftTitleLabel;
}
@end
